package com.hzit.servlet;

import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;
	// 初始化
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}
	// 每次发送请求都会调用
	@Override
	public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 查询所有学生
		List<Student> students = studentService.findAll();
		//2. 将学生集合放到请求作用域中
		req.setAttribute("students",students);
		//3. 转发到jsp页面，显示所有学生
		req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req,resp);
	}
	// 销毁servlet的方法
	@Override
	public void destroy() {
		System.out.println("destroy...");
	}
}
